#!/bin/bash
set -e

# Configuration
SOURCE_DIR="/home/packages/deepseek-chat-1.0.0"
TARGET_DIR="/home/deepseek-chat-1.0.0"
BACKUP_DIR="/home/backup/deepseek-chat-$(date +%Y%m%d%H%M%S)"

# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

log_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

log_warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# Check if source exists
if [ ! -d "$SOURCE_DIR" ]; then
    log_error "Source directory not found: $SOURCE_DIR"
    exit 1
fi

log_info "Starting deployment update..."

# Backup existing version if it exists
if [ -d "$TARGET_DIR" ]; then
    log_info "Backing up existing version to $BACKUP_DIR..."
    mkdir -p "$(dirname "$BACKUP_DIR")"
    mv "$TARGET_DIR" "$BACKUP_DIR"
else
    log_info "No existing version found at $TARGET_DIR"
fi

# Move new version
log_info "Moving new version from $SOURCE_DIR to $TARGET_DIR..."
# Ensure parent directory exists
mkdir -p "$(dirname "$TARGET_DIR")"
mv "$SOURCE_DIR" "$TARGET_DIR"

log_info "Deployment files updated successfully!"
log_info "New location: $TARGET_DIR"
